using System;
using System.Configuration;
using System.Web.UI;
using mojoPortal.Web;
using mojoPortal.Web.UI;
using mojoPortal.Business.WebHelpers;
using ud_Article.Business;
using ud_Utilities;
using System.Collections.Generic;
using System.Text;
using mojoPortal.Business;
using ud_ArticleCategory = ud_Article.Business.ud_ArticleCategory;

namespace ud_Article.UI
{

    public partial class ArticleMenuModule : SiteModuleControl
    {
        protected ModuleTitleControlCustom TitleControl;
        protected ArticleMenuConfiguration Config = new ArticleMenuConfiguration();
        readonly private DBArticleCategory _repository = new DBArticleCategory();

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Site != null && Site.DesignMode)
            {
                return;
            }
            Load += Page_Load;
            LoadSettings();
        }
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Site != null && Site.DesignMode)
            {
                return;
            }
            if (Page.IsPostBack) return;
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            PopulateMenu();
        }

        private void PopulateMenu()
        {
            StringBuilder sb = new StringBuilder();
            if (Config.ArticleModuleSelectorSetting != string.Empty)
            {
                string idAll = Config.ArticleModuleSelectorSetting.Remove(Config.ArticleModuleSelectorSetting.Length - 1);
                char[] param = {';'};
                string[] listModuleId = idAll.Contains(";") ? idAll.Split(param) : new[] {idAll};
                if (Config.UseAccordionSetting)
                {
                    sb.Append(Config.AccordionMouseHoverSetting
                                  ? "<div class='mojo-accordion-nh-h'>"
                                  : "<div class='mojo-accordion-nh'>");
                }
                foreach (string value in listModuleId) // Get moduleId, pageId in format [moduleId-pageId]
                {
                    int mid = int.Parse(value.Remove(value.IndexOf('-'))); //Get moduleId
                    int pid = int.Parse(value.Remove(0, value.IndexOf('-') + 1)); //Get pageId
                    Module m = new Module(mid);
                    if (Config.UseAccordionSetting)
                    {
                        sb.Append("<h3 class='header headerno" + mid + "'><a href='#'>" + m.ModuleTitle + "</a></h3>");
                        sb.Append("<div class='content contentno" + mid + "'>");
                    }
                    else
                    {
                        if (Config.ShowHeaderSetting)
                        {
                            sb.Append("<div class='header'><div class='l'></div><div class='m'>");
                            sb.Append(m.ModuleTitle);
                            sb.Append("</div><div class='r'></div></div>");
                        }
                    }
                    if (Config.UseArtisteerSetting)
                    {
                        sb.Append("<div class='art-nav'><div class='l'></div><div class='r'></div>");
                        sb.Append("<ul id='" + ModuleId + "' class='art-menu'>");
                    }
                    else
                    {
                        sb.Append("<ul id='" + ModuleId + "' class='treeview-menu'>");
                    }
                    List<ud_ArticleCategory> rootItems = _repository.GetRoot(mid);
                    PopulateHtmlMenu(ref rootItems, ref sb, pid, mid);
                    sb.Append("</ul>");
                    if (Config.UseArtisteerSetting)
                    {
                        sb.Append("</div>");
                    }
                    if (Config.UseAccordionSetting)
                    {
                        sb.Append("</div>");
                    }
                }
                if (Config.UseAccordionSetting)
                {
                    sb.Append("</div>");
                }
            }
            ltrMenu.Text = sb.ToString();
        }

        private void PopulateHtmlMenu(ref List<ud_ArticleCategory> root, ref StringBuilder sb, int pid, int mid)
        {
            foreach (ud_ArticleCategory item in root)
            {
                List<ud_ArticleCategory> children = _repository.GetChildren(item.CategoryID);
                if (children.Count > 0)
                {
                    sb.Append("<li><span><a href='" + SiteRoot + "/Article/ViewListByCategory.aspx?pageid=" + pid +
                              "&mid=" + mid + "&cat=" + item.CategoryID + "' >" +
                              Utilities.CutString(item.Category, Config.MaxNumberOfCharactersInTitleSetting,
                                                  Config.TitleSuffixSetting) + "</a></span>");
                    sb.Append("<div class='image'><img src='" + ImageSiteRoot + "/" + ConfigurationManager.AppSettings["ArticleImagesFolder"] + item.ImageUrl + "' alt='' /></div>");
                    sb.Append("<div class='description'>" + item.Description + "</div>");
                    sb.Append("<ul>");
                    PopulateHtmlMenu(ref children, ref sb, pid, mid);
                }
                else
                {
                    sb.Append("<li><span><a href='" + SiteRoot + "/Article/ViewListByCategory.aspx?pageid=" + pid +
                              "&mid=" + mid + "&cat=" + item.CategoryID + "' >" +
                              Utilities.CutString(item.Category, Config.MaxNumberOfCharactersInTitleSetting,
                                                  Config.TitleSuffixSetting) + "</a></span>");
                    sb.Append("<div class='image'><img src='" + ImageSiteRoot + "/" + ConfigurationManager.AppSettings["ArticleImagesFolder"] + item.ImageUrl + "' alt='' /></div>");
                    sb.Append("<div class='description'>" + item.Description + "</div>");
                }
                if (children.Count > 0)
                {
                    sb.Append("</ul>");
                    sb.Append("</li>");
                }
                else
                {
                    sb.Append("</li>");
                }
            }
        }

        private void PopulateLabels()
        {

        }

        private void SetJQueryScript()
        {
            if (Config.UseArtisteerSetting || !Config.UseTreeViewSetting) return;
            StringBuilder sb = new StringBuilder();
            sb.Append("<script src='" + ImageSiteRoot +
                      "/ClientScript/jqmojo/jquery.cookie.js' type='text/javascript'></script>");
            sb.Append("<script src='" + ImageSiteRoot +
                      "/ClientScript/jqmojo/jquery.treeview.min.js' type='text/javascript'></script>");
            sb.Append("<script type='text/javascript'>");
            sb.Append("$(document).ready(function(){");
            sb.Append("$('#" + ModuleId + "').treeview({");
            sb.Append("animated: 'fast', ");
            sb.Append("collapsed: true, ");
            sb.Append("unique: true, ");
            sb.Append("persist: 'cookie'});});");
            sb.Append("var s = window.location;");
            sb.Append("$('.treeview-menu a[href~=' + s + ']').addClass('current');");
            sb.Append("</script>");

            if (!Page.ClientScript.IsStartupScriptRegistered(typeof (Page), "articlemenu" + ModuleId))
            {
                Page.ClientScript.RegisterStartupScript(typeof (Page), "articlemenu" + ModuleId, sb.ToString());
            }
        }

        private void LoadSettings()
        {
            Config = new ArticleMenuConfiguration(Settings);
            SetJQueryScript();
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            pnlContainer.ModuleId = ModuleId;
            pnlContainer.CssClass = Config.InstanceCssClass;
        }
    }
}